package com.mars.module.admin.controller;


import com.mars.common.enums.BusinessType;
import com.mars.common.response.PageInfo;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.framework.annotation.Log;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysHouse;
import com.mars.module.admin.request.SysHouseRequest;
import com.mars.module.admin.service.ISysHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 房屋信息控制层
 *
 * @author mars
 * @date 2024-01-23
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "租房项目-房屋信息接口管理", tags = "租房项目-房屋信息接口管理")
@RequestMapping("/admin/sysHouse")
public class SysHouseController {

    private final ISysHouseService iSysHouseService;

    /**
     * 分页查询房屋信息列表
     */
    @ApiOperation(value = "分页查询房屋信息列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysHouse>> pageList(@RequestBody SysHouseRequest sysHouseRequest) {
        return R.success(iSysHouseService.pageList(sysHouseRequest));
    }

    /**
     * 获取房屋信息详细信息
     */
    @ApiOperation(value = "获取房屋信息详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysHouse> detail(@PathVariable("id") Long id) {
        return R.success(iSysHouseService.getById(id));
    }

    /**
     * 新增房屋信息
     */
    @Log(title = "新增房屋信息", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增房屋信息")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysHouseRequest sysHouseRequest) {
        iSysHouseService.add(sysHouseRequest);
        return R.success();
    }

    /**
     * 修改房屋信息
     */
    @Log(title = "修改房屋信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改房屋信息")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysHouseRequest sysHouseRequest) {
        iSysHouseService.update(sysHouseRequest);
        return R.success();
    }

    /**
     * 删除房屋信息
     */
    @Log(title = "删除房屋信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除房屋信息")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysHouseService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
     * 发布房屋信息
     */
    @Log(title = "发布房屋信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "发布房屋信息")
    @PostMapping("/publish/{ids}")
    public R publish(@PathVariable Long[] ids) {
        return iSysHouseService.publish(ids);
    }

    /**
     * 下架房屋信息
     */
    @Log(title = "下架房屋信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "下架房屋信息")
    @PostMapping("/offShelf/{ids}")
    public R offShelf(@PathVariable Long[] ids) {
        return iSysHouseService.offShelf(ids);
    }

    /**
     * 导出房屋信息
     *
     * @param response response
     * @throws IOException IOException
     */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response, @RequestBody SysHouseRequest sysHouseRequest) throws IOException {
        List<SysHouse> list = iSysHouseService.list(sysHouseRequest);
        ExcelUtils.exportExcel(list, SysHouse.class, "房屋信息信息", response);
    }
}
